//
//  ChatListViewCtrl.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/15.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "ChatListViewCtrl.h"
#import "ChatListTableView.h"
#import "RecentChatManager.h"
#import "ChatDetailViewCtrl.h"

#define NoDialogueView_Width (147.0)
#define NoDialogueView_hight (102.0)
#define LableWidth (200.0)
#define LableHight (25.0)

@interface ChatListViewCtrl () {
    UIView *statusView;
    UIImageView *warn;
    UILabel *warnLabel;
    UIView *_noChatView;

}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *chatTableTop;

@property (weak, nonatomic) IBOutlet ChatListTableView *chatListTableview;

@property (nonatomic,strong)NSMutableArray *chatListArrs;

@end

@implementation ChatListViewCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self fetchData];
    [self layoutChatListTableview];
    [self addNoChatView];
    [self testRefresh];
    self.navigationItem.title = @"消息";
    
    // 网络连接提示
    [self addNetWorkStatusView];
    
    [NSNotificationCenter addMessageListMsgNF:self action:@selector(handleMessageListMsg:)];
    [NSNotificationCenter addCmdConnStatusChangeNotification:self action:@selector(connStatusChange:)];
    
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    WeakSelf(ws);
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            [ws setStatusViewState:YES];
            NSLog(@"大哥，有网络啊");
        } else {
            [ws setStatusViewState:NO];
            NSLog(@"大哥，网络断了");
        }
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -  layout
- (void)layoutChatListTableview {
    WeakSelf(ws);
    //have no idea
    self.chatListTableview.didSelectedRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
        RecentChat *rchat = [ws.chatListArrs objectAtIndex:indexPath.row];

        ChatDetailViewCtrl *chatDetail = [[ChatDetailViewCtrl alloc] initWithNibName:@"ChatDetailViewCtrl" bundle:nil];
        chatDetail.rcChat = rchat;
        [ws.navigationController pushViewController:chatDetail animated:YES];
    };
}

#pragma mark - 界面
- (void)showNOChatViewOrNot {
    _noChatView.hidden = (self.chatListArrs.count>0);
}

// 添加空页面
- (void)addNoChatView
{
    CGFloat interval = 10;
    UIView *view= [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake((kScreenWidth -NoDialogueView_Width)/2, 0, NoDialogueView_Width, NoDialogueView_hight);
    imageView.image = [UIImage imageNamed:@"ddklNoDialogue"];
    [view addSubview:imageView];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake((kScreenWidth-LableWidth)/2, CGRectGetMaxY(imageView.frame)+interval, LableWidth, LableHight);
    label.font = [UIFont systemFontOfSize:16];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    label.text = @"have no message";
    [view addSubview:label];
    
    CGFloat bottom = label.bottom;
    CGRect viewRect = CGRectZero;
    viewRect.origin.x = 0;
    viewRect.origin.y = (kScreenHeight - 64 - 49 - bottom)/2.0f;
    viewRect.size.height = bottom;
    viewRect.size.width = kScreenWidth;
    view.frame = viewRect;
    
    _noChatView = view;
    [self.view addSubview:_noChatView];
}

// 网络连接提示
- (void)addNetWorkStatusView
{
    statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    [statusView setBackgroundColor:HEXCOLOR(0xe0e0e0)];
    [statusView setHidden:YES];
    
    warn = [[UIImageView alloc] initWithFrame:CGRectMake(90, 6, 32, 32)];
    [warn setImage:[UIImage imageNamed:@"warn"]];
    [statusView addSubview:warn];
    
    warnLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 9, 120, 25)];
    [warnLabel setFont:[UIFont systemFontOfSize:15]];
    [warnLabel setTextColor:HEXCOLOR(0x646464)];
    [warnLabel setBackgroundColor:[UIColor clearColor]];
    
    [statusView addSubview:warnLabel];
    [self.view addSubview:statusView];
}

- (void)setStatusViewState:(BOOL)isConnected
{
    if (isConnected) {
        [statusView setHidden:YES];
        self.chatTableTop.constant = 0;
        [warn setHidden:YES];
        [warnLabel setText:@"已连接"];
    }else {
        [statusView setHidden:NO];
        self.chatTableTop.constant = 44;
        [warn setHidden:NO];
        [warnLabel setText:@"网络连接不可用"];
    }
}

#pragma mark - fetchData
- (void)fetchData {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSMutableArray *recents = [[RecentChatManager sharedInstance] selectAllRecentData];
        self.chatListArrs  = recents;
        [self showNOChatViewOrNot];
    });
}

- (void)testRefresh
{
    RecentChat *nowchat = [[RecentChat alloc] init];
    nowchat.friendid = @"100";
    nowchat.friendName = @"宋旭华";
    nowchat.friendLogo = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503305143510&di=c15e57c0e315a2b847a1d3dee0835c2a&imgtype=jpg&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fe7cd7b899e510fb3b993648ad333c895d1430c20.jpg";
    nowchat.unreadCount = 10;
    nowchat.newerMsg = @"谢谢你关注我们门店的动态,请持续注意我们门店带来的最新产品和惊喜！这就是条测试数据" ;
    nowchat.updateTime = @"2015-12-13 11:34:25";
    nowchat.msgSourceType = CHAT_MSG_RECV;
    nowchat.msgBodyType = ChatMsgBodyType_Text;
    nowchat.msgState = CHAT_MSG_RECV_SUCC;
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:nowchat];
    self.chatListArrs = arr;
    [self showNOChatViewOrNot];
}

#pragma mark - 通知
- (void)handleMessageListMsg:(NSNotification *)not {

    
}

- (void)connStatusChange:(NSNotification *)not {
    NSDictionary* data = [not userInfo];
    //连接状态
    if ([[data objectForKey:@"ConnStatus"] intValue]==0) {
        
        [self setStatusViewState:YES];
    }else {
        [self setStatusViewState:NO];
    }

}

#pragma mark - setter
- (void)setChatListArrs:(NSMutableArray *)chatListArrs {
    if (_chatListArrs != chatListArrs) {
        _chatListArrs = chatListArrs;
        self.chatListTableview.chatLists = _chatListArrs;
        [self.chatListTableview reloadData];
    }
}


@end
